package com.tianji.learning.listeners;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class LessonRefundListener {
    final ILearningLessonService learningLessonService;
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "learning.lesson.refund.queue", durable = "true"),
        exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.ORDER_REFUND_KEY
    ))
    public void onMsg(OrderBasicDTO orderBasicDTO){
        //查看退款订单信息是否为null
        if(orderBasicDTO.getOrderId() == null
        || orderBasicDTO.getCourseIds() == null
        || orderBasicDTO.getUserId() == null
        || orderBasicDTO.getFinishTime() == null){
            return;
        }
        //不为空则删除退款的课程
        QueryWrapper<LearningLesson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",orderBasicDTO.getUserId()).eq("course_id",orderBasicDTO.getCourseIds().get(0));
        learningLessonService.remove(queryWrapper);
        log.info("课程id为{}，已经删除",orderBasicDTO.getCourseIds());
    }
}
